<?php
//$fotos equals the predefined folder, which contains all the sub folders
$fotos = "./Fotoalbum/";
$pictures = array();
$pictures[0][0][0] = $fotos;

function bilderzaehlen($i,$ordners,$fotos){	//puts the picture URL into the array
	global $pictures;
	$bilder = scandir($fotos.$ordners[$i]);
	$x = 2;
	for ($x = 2; $x < count($bilder); $x++) {
		if (exif_imagetype($fotos.$ordners[$i]."/".$bilder[$x]) == IMAGETYPE_GIF || IMAGETYPE_JPEG || IMAGETYPE_PNG || IMAGETYPE_BMP){
			$pictures[0][$i][$x] = $fotos.$ordners[$i]."/".$bilder[$x];
		}
	}
}

function ordnerzaehlen($fotos){ //puts the sub folders into the array
	global $pictures;
	$ordners = scandir($fotos);
	for ($i = 2; $i < count($ordners); $i++) {
		if (is_dir($fotos.$ordners[$i])){
			$pictures[0][$i][0] = $ordners[$i];
			bilderzaehlen($i,$ordners,$fotos);
		}
	}
}

ordnerzaehlen($fotos,$pictures);

$json = json_encode($pictures); //create JSON from array
echo $json;
?>